'''
https://leetcode.cn/problems/decode-string/description/
'''


class Solution:
    def decodeString(self, s: str) -> str:
        n = len(s)
        where = 0
        is_num = lambda x: x in {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
        is_str = lambda x: ord('a') <= ord(x) <= ord('z')
        def process(i):
            cur = ''
            time = 0
            nonlocal where
            while i < n and s[i] != ']':
                if is_str(s[i]):
                    cur += s[i]
                elif is_num(s[i]):
                    time = time * 10 + int(s[i])
                else:
                    cur += process(i + 1) * (time if time else 1)
                    time = 0
                    i = where
                i += 1
            where = i
            return cur * time if time else cur
        return process(0)

s = "3[a]2[bc]"
print(Solution().decodeString(s))